package com.jwdf.uda.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemQuizQuickBinding;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.QuizDetailAction;
import com.jwdf.uda.http.action.ReplyDetailAction;
import com.jwdf.uda.ui.activity.EditAnswerActivity;
import com.jwdf.uda.ui.activity.NewAnswerActivity;
import com.jwdf.uda.ui.activity.QuizDetailActivity;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.StringUtils;

import retrofit2.Response;

/**
 * Created by fatsfan on 2016/10/19.
 */

public class QuizQuickAdapter extends AbsAdapter<PbBean.PbBaseQuestion> {

    private Context context;
    private BaseFragment baseFragment;

    public QuizQuickAdapter(Context context, BaseFragment baseFragment) {
        super(context);
        this.context = context;
        this.baseFragment = baseFragment;
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_quiz_quick, parent, false));
    }

    private class ViewHolder extends AbsViewHolder<PbBean.PbBaseQuestion> {
        private ItemQuizQuickBinding mBinding;

        public ViewHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @SuppressLint("StringFormatMatches")
        @Override
        protected void onBindView(final PbBean.PbBaseQuestion item) {
            mBinding.tvTitle.setText(StringUtils.filterNullAndTrim(item.getTitle()));
            mBinding.tvCount.setText(convertView.getContext().getString(R.string.item_top_count, item.getPv(), item.getConcernCount(), item.getAnswerCount()));

            mBinding.getRoot().setOnClickListener(this);

            QuizDetailAction action = QuizDetailAction.newInstance(context, item.getQuestionId());
            baseFragment.onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResQuestionFindQuestion>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResQuestionFindQuestion> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    if(baseFragment.getView() != null) {
                        baseFragment.closeProgressDialog();
                        if(HttpHelper.showError(context, resultCode, baseServerInfo, R.string.error_operation)) {
                            return;
                        }

                        doSuccessQuistion(response.body());

                    }
                }
            }));

        }

        private void doSuccessQuistion(final PbBean.PbResQuestionFindQuestion body) {

            final int answerId = body.getAnswerId();

            mBinding.tvToReply.setText(answerId == 0 ? "去回答" : "编辑回答");

            View.OnClickListener onClick = new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(answerId == 0) {
                        NewAnswerActivity.start(context, item.getQuestionId(), item.getVoteOptionList().size(), item.getTitle());
                    } else {

                        ReplyDetailAction action = ReplyDetailAction.newInstance(context, answerId);
                        baseFragment.onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResQuestionFindAnswer>() {
                            @Override
                            public void onCallback(int resultCode, Response<PbBean.PbResQuestionFindAnswer> response, PbBean.PbBaseServerInfo baseServerInfo) {
                                if(baseFragment.getView() != null) {
                                    baseFragment.closeProgressDialog();
                                    if(HttpHelper.showError(context, resultCode, baseServerInfo, R.string.error_operation)) {
                                        return;
                                    }
                                    final PbBean.PbResQuestionFindAnswer body = response.body();
                                    doSuccessAnswer(body);
                                }
                            }
                        }));

                    }
                }
            };

            mBinding.tvToReply.setOnClickListener(onClick);
        }

        private void doSuccessAnswer(PbBean.PbResQuestionFindAnswer body) {
            EditAnswerActivity.start(context, body, item.getVoteOptionList().size());
        }

        @Override
        public void onClick(View v) {
            Context context = v.getContext();

            switch(v.getId()) {

                case R.id.vRoot:
                    boolean isAuthor = TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(convertView.getContext()));
                    QuizDetailActivity.start(context, item.getQuestionId(), isAuthor);
                    break;

            }

        }
    }

}
